<template>
  <div class="register-container">
    <div class="register-form">
      <div class="register-header">
        <h2 class="register-title">创建账号</h2>
        <p class="register-subtitle">加入社团管理系统，开始您的社团之旅</p>
      </div>
      
      <form @submit.prevent="handleRegister"
        class="form"
      >
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="formData.username"
            placeholder="请设置用户名"
            required
            class="form-input"
          />
        </div>
        <div class="form-group">
          <label for="email">邮箱</label>
          <input
            type="email"
            id="email"
            v-model="formData.email"
            placeholder="请输入邮箱"
            required
            class="form-input"
          />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="formData.password"
            placeholder="请设置密码"
            required
            class="form-input"
          />
        </div>
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            type="password"
            id="confirmPassword"
            v-model="formData.confirmPassword"
            placeholder="请再次输入密码"
            required
            class="form-input"
          />
          <p v-if="formData.password !== formData.confirmPassword && formData.confirmPassword"
            class="error-message"
          >两次输入的密码不一致</p>
        </div>
        <div class="form-options">
          <div class="agree-terms">
            <input
              type="checkbox"
              id="terms"
              v-model="formData.agreeTerms"
              required
            />
            <label for="terms">
              我已阅读并同意 
              <a href="#" class="terms-link">用户协议</a> 和 
              <a href="#" class="terms-link">隐私政策</a>
            </label>
          </div>
        </div>
        <button type="submit" 
          class="register-button" 
          :disabled="isLoading || formData.password !== formData.confirmPassword"
        >
          {{ isLoading ? '注册中...' : '注册' }}
        </button>
        <div class="login-link">
          已有账号？
          <a href="/login">立即登录</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 表单数据
const formData = ref({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  agreeTerms: false
})

const isLoading = ref(false)

// 处理注册
const handleRegister = async () => {
  // 检查密码是否一致
  if (formData.value.password !== formData.value.confirmPassword) {
    return
  }
  
  isLoading.value = true
  try {
    // 在实际项目中，这里应该调用API进行注册
    // 这里使用模拟数据进行演示
    // const response = await api.register(formData.value)
    
    // 模拟注册成功
    setTimeout(() => {
      // 注册成功后跳转到登录页
      router.push({
        path: '/login',
        query: { registered: 'success' }
      })
    }, 1500)
  } catch (error) {
    console.error('注册失败:', error)
    // 实际项目中应该显示错误提示
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 20px;
}

.register-form {
  background-color: white;
  border-radius: 12px;
  padding: 40px;
  width: 100%;
  max-width: 480px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.register-header {
  text-align: center;
  margin-bottom: 32px;
}

.register-title {
  font-size: 24px;
  font-weight: 700;
  color: #333;
  margin-bottom: 8px;
}

.register-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.form-input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.3s;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.error-message {
  color: #e53935;
  font-size: 12px;
  margin-top: 6px;
  margin-bottom: 0;
}

.form-options {
  margin-bottom: 24px;
}

.agree-terms {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 14px;
  color: #666;
}

.agree-terms input {
  margin-top: 3px;
}

.terms-link {
  color: #667eea;
  text-decoration: none;
  transition: color 0.3s;
}

.terms-link:hover {
  color: #764ba2;
  text-decoration: underline;
}

.register-button {
  width: 100%;
  padding: 12px;
  background-color: #667eea;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s;
}

.register-button:hover:not(:disabled) {
  background-color: #764ba2;
}

.register-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}

.login-link a {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s;
}

.login-link a:hover {
  color: #764ba2;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .register-form {
    padding: 30px 20px;
  }
  
  .agree-terms {
    flex-direction: column;
    align-items: flex-start;
  }
}
</style>